package com.codedancer.campus.manager.api.headlineNews;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.common.anno.InterfacePermission;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.response.ApiResponse;
import com.codedancer.campus.manager.api.headlineNews.ro.HeadlineNewsPageSearchRo;
import com.codedancer.campus.manager.service.IHeadlineNesService;
import com.codedancer.campus.manager.service.bo.headlineNews.HeadlineNewsPageSearchBo;
import com.codedancer.campus.manager.service.vo.headlineNews.HeadlineNewsPageVo;
import com.codedancer.campus.manager.service.vo.headlineNews.HeadlineNewsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/headline-news")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "头条新闻Api", description = "头条新闻Api", tags = "头条新闻Api")
public class HeadlineNesApi extends BaseApi {

    @Autowired
    private IHeadlineNesService headlineNesService;

    @GetMapping("/page")
    @ApiOperation("分页查询头条新闻")
    @InterfacePermission(encodes = {"superAdmin","headlineNewsAdmin"})
    public ApiResponse<IPage<HeadlineNewsPageVo>> page(
        @ApiParam(value = "头条新闻搜索Ro", required = false)
        HeadlineNewsPageSearchRo headlineNewsPageSearchRo) {
        HeadlineNewsPageSearchBo headlineNewsPageSearchBo = BeanUtil.copyProperties(
            headlineNewsPageSearchRo, HeadlineNewsPageSearchBo.class);
        return success(headlineNesService.page(headlineNewsPageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("头条新闻详情")
    @InterfacePermission(encodes = {"superAdmin","headlineNewsAdmin"})
    public ApiResponse<HeadlineNewsVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "新闻uuid", required = true) String uuid) {
        return success(headlineNesService.detail(uuid));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除头条新闻")
    @InterfacePermission(encodes = {"superAdmin","headlineNewsAdmin"})
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "新闻uuid", required = true) String uuid) {
        headlineNesService.delete(uuid);
        return success();
    }
}
